package pers.vinsonws.jianghu.clientwin.handler;

import io.netty.channel.ChannelHandlerContext;
import pers.vinsonws.jianghu.clientwin.global.Info;
import pers.vinsonws.jianghu.clientwin.model.ChatRoomClient;
import pers.vinsonws.jianghu.clientwin.model.UserClient;
import pers.vinsonws.jianghu.clientwin.ui.ApplyListUi;
import pers.vinsonws.jianghu.clientwin.ui.ChatOneUi;
import pers.vinsonws.jianghu.clientwin.ui.ChatRoomUi;
import pers.vinsonws.jianghu.codeCompont.MsgEveryPro;
import pers.vinsonws.jianghu.tools.DivisMessage;

/**
 * @auther vinsonws
 * @date 2018/11/11 23:57
 */
public class ClientMainReadProcess {

    public static void readProcess(MsgEveryPro.Message message, ChannelHandlerContext ctx) {
        String logger = null;
        switch (message.getType()) {
            case 0:
                initClient(message);
                logger = "初始化客户端-更新聊天室列表";
                break;
            case 1:
                registerUser(message);
                logger = "注册";
                break;
            case 2:
                loginUser(message);
                logger = "登录";
                break;
//            case 3:
//                applyRoom(message);
//                logger = "申请聊天室";
//                break;
//            case 4:
//                dealApply(message);
//                logger = "处理申请";
//                break;
//            case 5:
//                alterInfo(message, session);
//                logger = "更改资料";
//                break;
//            case 6:
//                loginOut();
//                logger = "登出";
//                break;
            case 7:
                chatOneToOne(message);
                logger = "聊天-一对一";
                break;
            case 8:
                chatRoom(message);
                logger = "聊天-聊天室";
                break;
//            case 9:
//                userLookApplyHerself(message);
//                logger = "用户查看申请";
//                break;
            case 10:
                adminLookApplyHerself(message);
                logger = "管理员查看申请";
                break;
//            case 11:
//                adminOperateApply(message);
                //logger = "管理员操作申请";
                //break;
            case 12:
                onlineUsers(message);
                logger = "初始化客户端-更新在线用户列表";
                break;
            case 13:
                offlineUser(message);
                logger = "用户下线通知";
                break;
            default: //暂时不做处理
                logger = "无效处理类型";
                break;
        }
        System.out.println(ctx.channel().remoteAddress().toString() + "正在进行" + logger);

    }

    /** 0
     *
     * @param message
     */
    private static void initClient(MsgEveryPro.Message message){
        DivisMessage divisMessage = new DivisMessage(message.getMessage(), message.getType());
        for (int i :Info.chatRoomClientHashMap.keySet()){
            if (i == Integer.parseInt(divisMessage.getStrByIndex(1))){
                Info.chatRoomClientHashMap.remove(i);
                break;
            }
        }
        ChatRoomClient chatRoomClient = new ChatRoomClient();
        chatRoomClient.setId(Integer.parseInt(divisMessage.getStrByIndex(1)));
        chatRoomClient.setName(divisMessage.getStrByIndex(2));
        chatRoomClient.setBelongToMe(divisMessage.getStrByIndex(3).equals("1"));
        chatRoomClient.setMaster(divisMessage.getStrByIndex(4).equals("1"));
        Info.chatRoomClientHashMap.put(chatRoomClient.getId(), chatRoomClient);
        Info.initStatus = true;
        Info.indexUi.paintChatRoom();
    }
    /** 1
     *
     * @param message
     */
    private static void registerUser(MsgEveryPro.Message message){
        DivisMessage divisMessage = new DivisMessage(message.getMessage(), message.getType());
        Info.uid = divisMessage.getStrByIndex(1);
    }

    /** 2
     *
     * @param message
     */
    private static void loginUser(MsgEveryPro.Message message){
        DivisMessage divisMessage = new DivisMessage(message.getMessage(), message.getType());
        if (divisMessage.getLength() > 1){
            Info.status = 1;
            Info.id = Integer.parseInt(divisMessage.getStrByIndex(1));
            Info.userName = divisMessage.getStrByIndex(2);
        }
    }

    /** 7
     *
     * @param message
     */
    private static void chatOneToOne(MsgEveryPro.Message message){
        if (message.getChatRoomOrUserRecvId() == 0) return;
        DivisMessage divisMessage = new DivisMessage(message.getMessage(), message.getType());
        ChatOneUi chatOneUi = Info.userOpened.get(message.getUserSendId());
        if (chatOneUi == null) {//这里要想一个好办法当用户没打开界面是的对策
            ChatOneUi chatOneUi1 = new ChatOneUi();
            chatOneUi1.openChatOneWin(message.getUserSendId(), divisMessage.getStrByIndex(1));
            Info.userOpened.put(message.getUserSendId(), chatOneUi1);
            chatOneUi1.addChatText(divisMessage.getStrByIndex(1), message.getDateLong(),
                    divisMessage.getStrByIndex(2));
            return;
        }
        chatOneUi.addChatText(divisMessage.getStrByIndex(1), message.getDateLong(),
                divisMessage.getStrByIndex(2));
    }

    /** 8
     *
     * @param message
     */
    private static void chatRoom(MsgEveryPro.Message message){
        if (message.getUserSendId() == Info.id) return;
        DivisMessage divisMessage = new DivisMessage(message.getMessage(), message.getType());
        ChatRoomUi chatRoomUi = Info.chatRoomOpened.get(message.getChatRoomOrUserRecvId());
        if (chatRoomUi == null) return;
        chatRoomUi.addChatText(divisMessage.getStrByIndex(1), message.getDateLong(), divisMessage.getStrByIndex(2));
    }

    /** 10
     *
     * @param message
     */
    private static void adminLookApplyHerself(MsgEveryPro.Message message){
        DivisMessage divisMessage = new DivisMessage(message.getMessage(), message.getType());
        if (message.getChatRoomOrUserRecvId() == 0) return;
        ApplyListUi applyListUi = Info.adminOpreate.get(message.getChatRoomOrUserRecvId());
        if (applyListUi == null) return;
        applyListUi.adimOperateApply(Integer.parseInt(divisMessage.getStrByIndex(1)),
                message.getChatRoomOrUserRecvId());
    }

    /** 12
     *
     * @param message
     */
    private static void onlineUsers(MsgEveryPro.Message message){
        DivisMessage divisMessage = new DivisMessage(message.getMessage(), message.getType());
        //自己

        for (int i :Info.userClientHashMap.keySet()){
            if (i == Integer.parseInt(divisMessage.getStrByIndex(1))){
                Info.userClientHashMap.remove(i);
                break;
            }
        }
        UserClient userClient = new UserClient();
        userClient.setId(Integer.parseInt(divisMessage.getStrByIndex(1)));
        userClient.setName(divisMessage.getStrByIndex(2));
        Info.userClientHashMap.put(userClient.getId(), userClient);
        Info.initUserStatus = true;
        Info.onlineUserUi.paintOnlineUser();
    }

    /** 13
     *
     * @param message
     */
    private static void offlineUser(MsgEveryPro.Message message){
        DivisMessage divisMessage = new DivisMessage(message.getMessage(), message.getType());
        Info.userClientHashMap.remove(Integer.parseInt(divisMessage.getStrByIndex(1)));
        Info.initUserStatus = true;
        Info.onlineUserUi.paintOnlineUser();
    }
}